<?php
namespace My\Controller;

use Utuo\BackgroundController;

class SiteConfigController extends BackgroundController {
	
	protected function _initialize(){
		$this->setControllerTitle("微网站设置");
		$this->setActionTitle(array(
				'index' => '基础信息设置',
		));
	}
	
	// 用户设置
	public function indexAction() {
		$m = M("Site");
		if (!$m->where("id=" . $this->aid)->count()) {
			$m->add(array(
					'id' => $this->aid 
			));
		}
		
		$site = $m->find($this->aid);
		$modelId = M("Model")->where("tag='Site'")->getField("id");
		$themeLists = M()->table("(select this0.* from ( 
								  select * 
								  from __THEME__ this0 
								  where (this0.partner in ( SELECT partner FROM __CUSTOMER__ where id = {$this->aid} ) or this0.partner = 0) and this0.model_id=$modelId
								) this0
								where this0.customer in(0, {$this->aid})) this1")->getField("this1.id, this1.name");
		$site["themesOptions"] = $this->getOption($themeLists, $site['theme'], "==请选择主题==");
		$this->assign("site", $site);
		$this->display();
	}
	
	// 提交设置
	public function indexPostAction() {
		$d = D("Site");
		if ($d->create()) {
			$d->id = $this->aid;
			if (false !== $d->save()) {
				$this->ajaxSuccess();
			} else {
				$this->ajaxError($d->getError());
			}
		} else {
			$this->ajaxError($d->getError());
		}
	}
	
}